<?php
namespace Erp\c\Orders\Tn\Api;

//运输方式API交互基类
class ApiShippingModule{
	/**
	 *多线程http请求
	 *@param array $request array('id'=>array('url'=>'www.xx.com','post'=>'data','header'=>'xxx'))
	 */
	protected function multiCurl($request,$callback=false){
		$mh = curl_multi_init();
		foreach ($request as $i => $v) {
			$timeOut = (isset($v['timeout']) && $v['timeout']>0 )? $v['timeout'] :120;
			$conn[$i] = curl_init();
			curl_setopt($conn[$i], CURLOPT_URL,$v['url']);
			if($v['is_http']){
				curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, 1);
				curl_setopt($conn[$i], CURLOPT_SSL_VERIFYHOST, 1);
			}else{
				curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, 0);
				curl_setopt($conn[$i], CURLOPT_SSL_VERIFYHOST, 0);
			}
			if(isset($v['header']) && !empty($v['header'])){
				curl_setopt($conn[$i], CURLOPT_HTTPHEADER, $v['header']);
			}
			if(isset($v['post']) && !empty($v['post'])){
				$post = is_array($v['post']) ? http_build_query($v['post']) :$v['post'];
				curl_setopt($conn[$i], CURLOPT_POST, 1);
				curl_setopt($conn[$i], CURLOPT_POSTFIELDS, $post);
			}else{
				curl_setopt($conn[$i], CURLOPT_POST, 0);
			}
			curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,$timeOut);
			curl_setopt($conn[$i], CURLOPT_TIMEOUT,200);
			curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER,1);
			curl_multi_add_handle ($mh,$conn[$i]);
		}
		$running = null ;
		 // 执行批处理句柄
		 do {
			 usleep (5000);
			 curl_multi_exec ( $mh , $running );
		} while ( $running  >  0 );


		/*do {
			$mrc = curl_multi_exec($mh, $active);
		} while ($mrc == CURLM_CALL_MULTI_PERFORM);

		while ($active and $mrc == CURLM_OK){
			if (curl_multi_select($mh) != -1){
				do {
					$mrc = curl_multi_exec($mh, $active);
				} while ($mrc == CURLM_CALL_MULTI_PERFORM);
			}
		}*/
		$r = array();
		foreach ($request as $i => $v) {
			$data = curl_multi_getcontent($conn[$i]);
			curl_multi_remove_handle($mh,$conn[$i]);
			curl_close($conn[$i]);
			if($callback){
				if(is_array($callback)){
					$fun = $callback[1];
					$r[$i] = $callback[0]->$fun($data,$request[$i]);	//回调函数处理返回内容
				}else{
					$r[$i] = $callback($data,$request[$i]);
				}
			}else{
				$r[$i] = $data;
			}
		}
		curl_multi_close($mh);
		return $r;
	}

	/**
	 *单线程http请求
	 *@param
	 */
	protected function oneHttpRequest($url,$data='',$method='POST',$headers='',$timeOut=80,$isHttps=1,$testTime = 0){
		$connection = curl_init();
		curl_setopt($connection, CURLOPT_URL, $url);
		curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, $isHttps);
		curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, $isHttps);
		if($headers){
			curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
		}
		if($method=='POST'){
			curl_setopt($connection, CURLOPT_POST, 1);
			curl_setopt($connection, CURLOPT_POSTFIELDS, $data);
		}elseif($method=='DELETE'){
			curl_setopt($connection, CURLOPT_CUSTOMREQUEST,"DELETE");
            curl_setopt($connection, CURLOPT_POSTFIELDS, $data);
		}
		else{
			curl_setopt($connection, CURLOPT_HTTPGET, true);
		}
		curl_setopt($connection, CURLOPT_CONNECTTIMEOUT,$timeOut);
		curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
		if($testTime){
			curl_setopt($connection, CURLOPT_TIMEOUT_MS,$testTime);
		}
		$response = curl_exec($connection);
		curl_close($connection);
		return $response;
	}

	/**
	 *XML编码
	 *@param
	 */
	protected function xmlEncode($p, $act){
		return '<?xml version="1.0" encoding="utf-8"?>'.
				'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
			<soap:Body>
				<'.$act.' xmlns="http://shippingapi.ebay.cn/">
					<'.$act.'Request>
						'.$this -> _xmlEncode($p).'
					</'.$act.'Request>
				</'.$act.'>
			</soap:Body>
		</soap:Envelope>';
	}
	protected function _xmlEncode($p){
		$r = '';
		foreach ($p as $k=>$v){
			$attr = '';
			if(is_array($v) || is_object($v)){
				//---------------------------
				if(is_array($v) || is_object($v)){
					if(!is_numeric(key($v))){
						$v = $this->_xmlEncode($v);
					}else{
						foreach ($v as $content){
							if(is_array($content) || is_object($content)){
								$r .= "<$k>".($this->_xmlEncode($content))."</$k>\n";
							}else{
								$r .= "<$k>$content</$k>\n";
							}
						}
						return $r;
					}
				}
			}
			$r .= '<'.$k.$attr.'>'.$v."</$k>\n";
		}
		return $r;
	}

	/**
	 *XML解码
	 *@param
	 */
	public function xmlDecode($xml,$toArra=false)
	{
		//XML属性分析
		$xml = preg_replace('/(<.*?>)\s*(<.*?>)/', '$1'."\n".'$2' , $xml);
		$xml = preg_replace('/(<\w.*?>)\s(<\/.*?>)/', '$1EOS_NULL_EOL$2' , $xml);//++++++++++
		/* $xml = preg_replace('/(<.*?>)\s{2,}(<.*?>)/', '$1'."\n".'$2' , $xml);*/
		$xml = preg_replace('/(<\w*\s+\w*=.*?>)(.*?)(<.*?>)/','$1<val>$2</val>$3', $xml);

		$r = str_replace('EOS_NULL_EOL','',json_encode(simplexml_load_string($xml)));

		return json_decode($r,$toArra);
	}

	//数组对象转换成数组
	public function _objToArr($obj){
		$_arr = is_object($obj) ? get_object_vars($obj) :$obj;
		foreach ($_arr as $key=>$val){
			$val = (is_array($val) || is_object($val)) ? ($this->_objToArr($val)):$val;
			$arr[$key] = $val;
		}
		return $arr;
	}

}




